import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

from Django_Admin.user_settings import Headless_Chrome

StackOverflow = 'https://stackoverflow.com/questions/59534028/sign-in-to-gmail-account-fails-selenium-automation/59569816#59569816'

class AutoLogin(object):
    Chrome_Agent = 'user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36'
    chrome_options = Options()
    chrome_options.add_argument("--profile-directory=Default")
    chrome_options.add_argument("--whitelisted-ips")
    chrome_options.add_argument("--start-maximized")
    chrome_options.add_argument("--disable-extensions")
    chrome_options.add_argument("--disable-plugins-discovery")
    if Headless_Chrome == True:
        # 此处开启后为无头浏览器
        chrome_options.add_argument('--headless')
        chrome_options.add_argument('--no-sandbox')
        chrome_options.add_argument('--disable-dev-shm-usage')
        chrome_options.add_argument('blink-settings=imagesEnabled=false')
        chrome_options.add_argument('--disable-gpu')
    chrome_options.add_argument(Chrome_Agent)
    site = 'https://www.amazon.com/'

    cookieList = [{'domain': '.amazon.com', 'expiry': 1634208648, 'httpOnly': False, 'name': 'session-id', 'path': '/', 'secure': True, 'value': '133-4231767-0386665'}, {'domain': '.amazon.com', 'expiry': 1634208642, 'httpOnly': False, 'name': 'lc-main', 'path': '/', 'secure': False, 'value': 'zh_CN'}, {'domain': '.amazon.com', 'expiry': 1634208642, 'httpOnly': False, 'name': 'x-main', 'path': '/', 'secure': False, 'value': 'sk1INwGTa3ZzL8mB1Titmt6XTqmNmVd5fPsWbTkjNdQS42UrvJoSAT8ZD27nSMQv'}, {'domain': '.amazon.com', 'expiry': 2082787201, 'httpOnly': False, 'name': 'session-token', 'path': '/', 'secure': False, 'value': 'CpfOd3j1cCqVsjzExkFHFruhX1aKVx9KC5Ey/SygM54RCoWCYJf326hnrUoyenm5icjJBdxuGC4x495z51dXN7uOdWvNSJsZJTYv8c0hxYJ8Spq6mVBWBgVOMw6n3ZboT4gIn6u5fqevbXtn3nRNGwUm4q4Nw+TLcm9mZvwdRw0Us+XAQITl7uoHlTC0AXxX5AYNCOPZk8xJvp0/ZRIb1gXmXqpNQtNma5WQDRrfeFIDeU772DlbuR4fzqkmtRlj2jInABLqBEb3tQGFjoUt0e7xr6FWKlLp'}, {'domain': '.amazon.com', 'httpOnly': False, 'name': 'skin', 'path': '/', 'secure': False, 'value': 'noskin'}, {'domain': '.amazon.com', 'expiry': 1634208641, 'httpOnly': True, 'name': 'sess-at-main', 'path': '/', 'secure': True, 'value': '"JEf1xXgHQW2Q4bV5rYIvTg8ugSNmaXreyO82GVhKp2M="'}, {'domain': '.amazon.com', 'expiry': 1634208641, 'httpOnly': True, 'name': 'sst-main', 'path': '/', 'secure': True, 'value': 'Sst1|PQHu3atm6g3XIXTKnIf5r-a4CY3BBS_CDHf1neFiDg7e0AaWuE7G0KA3SrAgyvgqWZ-T5at__qiZM1k8W1la2E_hEVtn1EE82EU57vevfXRB92dMJkDu2OLJyEgmlKMs36CM6plgIoxoo9mEzzUirYNaiNj6puewlFPZ5up0x0EmC_PR2upHhDO8z_C4O5SDa7mghqlAUSa_WPk8zWW86yxgM0doL9IxpvZn55ys1nnSCkSXabbFNKZKU21Bic2qRXh4B-pPQXP620o_1QChzR_jYiVvdDHVMJWBFeYhKWDPd4g'}, {'domain': '.amazon.com', 'expiry': 1634208641, 'httpOnly': True, 'name': 'at-main', 'path': '/', 'secure': True, 'value': 'Atza|IwEBIO_rC2yMtbaPg6D9KbEjWmd6bSPyjEAPdu-TZMfDLwbRYrSk7fvkjDfVNfXleBY87H9zxUiK25zHa9DM6oTr9R-McAZN5xPsvY4WIoJXrnceV75eGl49NMcEm6PDwuToKxQ6sXkZvTLK0BPvYad0CPkjIgINDvwnYyV7CSnseBxFAzyt189ssHwzWA0I8WpFrFFJxjMmJE7TIBxwoAqA9JjrLhe8mV_IjeTDnxdzR6WQ5TrpOwrLIayBDFN7qtpB4Sc'}, {'domain': '.amazon.com', 'expiry': 1634208643, 'httpOnly': True, 'name': 'sp-cdn', 'path': '/', 'secure': True, 'value': '"L5Z9:CN"'}, {'domain': '.amazon.com', 'expiry': 1634208648, 'httpOnly': False, 'name': 'session-id-time', 'path': '/', 'secure': False, 'value': '2082787201l'}, {'domain': '.amazon.com', 'expiry': 1634208648, 'httpOnly': False, 'name': 'ubid-main', 'path': '/', 'secure': True, 'value': '131-3554967-1612700'}, {'domain': '.amazon.com', 'expiry': 1634208642, 'httpOnly': False, 'name': 'i18n-prefs', 'path': '/', 'secure': False, 'value': 'USD'}, {'domain': 'www.amazon.com', 'expiry': 1632912643, 'httpOnly': False, 'name': 'csm-hit', 'path': '/', 'secure': False, 'value': 'tb:s-NBF6MMRPXQQQ7G9YRMA3|1602672642552&t:1602672643334&adb:adblk_no'}]

    def __init__(self):
        pass

    def Selenium(self):
        print(self.site)
        print('======================================================')
        print('--------------  selenium  start  ---------------------')
        print('======================================================')
        # web = webdriver.Firefox(executable_path='/usr/local/bin/geckodriver')
        web = webdriver.Chrome('/usr/local/bin/chromedriver', options=self.chrome_options)
        # web.get('file:///Users/ericaaron/Desktop/aa/google-selenium.html')
        web.get(self.site)
        for cookie in self.cookieList:
            web.add_cookie(cookie)
        print("success")
        time.sleep(5)
        web.get(self.site)
        web.implicitly_wait(30)
        web.maximize_window()

        with open("../amazon-selenium.html", "w") as f:
            f.write(web.page_source)
        print("Selenium Done!")
        print('======================================================')
        print('--------------  selenium  Over  ----------------------')
        print('======================================================')
        time.sleep(1000)
        web.quit()

if __name__ == '__main__':
    auto = AutoLogin()
    auto.Selenium()
